﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ncp.Business;
using ncp.Business.Common;
using ncp.Business.FrendLink;


namespace ncp.Web.cms.work.FLinkManager
{
    public partial class FLink_Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
            if (userInfo == null)
            {
                Response.Write(" <script> top.location.href='/cms/login.aspx'; </script> ");
            }
            AjaxPro.Utility.RegisterTypeForAjax(typeof(FLink_Index));
        }

        //得到总记录数
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public int GetRecordCount()
        {
            return FrendLinkBll.GetFrendLinkCount("", CommonConst.DB_Name);
        }

        //装载商品表视图
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string BindTable(string jsonData, string jsonWhere)
        {
            var dictList = JsonHelper.JSONToObject<Dictionary<string, object>>(jsonData);
            var dicWhere = new Dictionary<string, object>();
            DataTable dt = FrendLinkBll.GetFrendLinkTable(int.Parse(dictList["currentpageindex"].ToString()) - 1, int.Parse(dictList["pagesize"].ToString()), "*", dictList["sorting"] + " " + dictList["sortdir"], dicWhere, CommonConst.DB_Name);
            var json = JsonHelper.DataTableToJSON(dt);
            return json;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string OnSave(string json)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
            if (userInfo != null)
            {
                try
                {
                    var dicProj = JsonHelper.JSONToObject<Dictionary<string, object>>(json);
                    var description = Server.HtmlEncode(dicProj["Link_Info"].ToString()).Replace("/", "");
                    dicProj.Remove("Link_Info");
                    dicProj.Add("Link_Info", description);
                    if (dicProj["Link_Id"].ToString() == "")
                    {
                        //添加
                        dicProj.Remove("Link_Id");
                        dicProj.Add("Link_Status",1);
                        FrendLinkBll.InsertFrendLink(dicProj, CommonConst.DB_Name);
                        jsonResult.Add("isreload", true);
                        jsonResult.Add("msg", "添加成功!");
                    }
                    else
                    {
                        //修改
                        Dictionary<string, object> dicWhere = new Dictionary<string, object>();
                        dicWhere.Add("Link_Id",dicProj["Link_Id"]);
                        dicProj.Remove("Link_Id");
                        FrendLinkBll.UpdateFrendLink(dicProj,dicWhere, CommonConst.DB_Name);

                        Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                        dicHandleLog.Add("user_id", userInfo.Id);
                        dicHandleLog.Add("computername", userInfo.Computername);
                        dicHandleLog.Add("computerip", userInfo.Computerip);
                        dicHandleLog.Add("handle_note", "修改友情链接");
                        dicHandleLog.Add("obj_guid", "Link_Id=" + dicWhere["Link_Id"].ToString());
                        LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                        jsonResult.Add("isreload", true);
                        jsonResult.Add("msg", "修改成功!");
                    }
                }
                catch (Exception ex)
                {
                    jsonResult.Clear();
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", ex.Message);
                }
            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "登录超时，请退出重新登录！");
            }

            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //删除商品信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string DeleteNewsInfo(int Link_Id)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                int ii = FrendLinkBll.DeleteFrendLink(Link_Id.ToString(), CommonConst.DB_Name);
                var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                dicHandleLog.Add("user_id", userInfo.Id);
                dicHandleLog.Add("computername", userInfo.Computername);
                dicHandleLog.Add("computerip", userInfo.Computerip);
                dicHandleLog.Add("handle_note", "删除友情链接");
                dicHandleLog.Add("obj_guid", "Link_Id=" + Link_Id.ToString());
                LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                jsonResult.Add("msg", "删除成功！");
                jsonResult.Add("isreload", true);
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //修改广告状态
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string SetNewsStatus(int Link_Id, int status)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            Dictionary<string, object> dicNews = new Dictionary<string, object>();
            Dictionary<string, object> dicWhere = new Dictionary<string, object>();
            dicWhere.Add("Link_Id", Link_Id);
            dicNews.Add("Link_Status", status);
            try
            {
                FrendLinkBll.UpdateFrendLink(dicNews, dicWhere, CommonConst.DB_Name);
                var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                dicHandleLog.Add("user_id", userInfo.Id);
                dicHandleLog.Add("computername", userInfo.Computername);
                dicHandleLog.Add("computerip", userInfo.Computerip);
                dicHandleLog.Add("handle_note", ((status == 0) ? "禁用广告" : "启用广告"));
                dicHandleLog.Add("obj_guid", "Link_Id=" + Link_Id.ToString());
                LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);

                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", "修改成功!");
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
